package com.fyzk.activity.welcome;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fyzk.activity.main.MainGroupActivityI;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.base.BaseActivity;
import com.fyzk.data.preference.MySharedPreferences;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 17/6/12.
 */

public class AndyViewPagerActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private Context context = this;
    private ViewPager advPager;
    private ViewPagerAdapter vpAdapter;
    private List<View> views;
    private ImageButton button;

    private static final int[] pics = {R.drawable.wellcome1, R.drawable.wellcome2,
            R.drawable.wellcome3, R.drawable.wellcome4};

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_layout);
        button = (ImageButton) findViewById(R.id.button);
        views = new ArrayList<View>();

        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        for (int i = 0; i < pics.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);
            iv.setImageResource(pics[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            views.add(iv);
        }
        advPager = (ViewPager) findViewById(R.id.viewpager);
        vpAdapter = new ViewPagerAdapter(views);
        advPager.setAdapter(vpAdapter);
        advPager.addOnPageChangeListener(this);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                goMain();
            }
        });
    }

    private void goMain() {
        MySharedPreferences.getInstance().setFirst(false);
        if (!MySharedPreferences.getInstance().getLoginStatus()) {
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
            finish();
        }else{
            Intent intent = new Intent(context, MainGroupActivityI.class);
            startActivity(intent);
            finish();
        }
    }

    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        advPager.setCurrentItem(position);
    }

    private boolean isScrolled;

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_DRAGGING:
                isScrolled = false;
                break;
            case ViewPager.SCROLL_STATE_SETTLING:
                isScrolled = true;
                break;
            case ViewPager.SCROLL_STATE_IDLE:
                if (advPager.getCurrentItem() == (pics.length - 1) && !isScrolled) {
                    goMain();
                }
                isScrolled = true;
                break;
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int arg0) {
        if (arg0 == pics.length - 1) {
            button.setVisibility(View.VISIBLE);
        } else {
            button.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        setCurView(position);
    }
}
